package cn.anc.aonicardv.module.adpter.recorder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.FileBean;
import cn.anc.aonicardv.module.adpter.listener.OnRecorderAlbumCancelDownloadClickListener;
import cn.anc.aonicardv.util.DataClearManager;
import cn.anc.aonicardv.widget.ProgressImageView;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.cache.CacheManager;

/**
 * Created by yangdai on 2017/12/4.
 */

public class RecorderDownloadAdapter extends RecyclerView.Adapter {

    private List<? extends FileBean> downloadList;
    public OnRecorderAlbumCancelDownloadClickListener onRecorderAlbumCancelDownloadClickListener;

    public RecorderDownloadAdapter(List<? extends FileBean> downloadList, OnRecorderAlbumCancelDownloadClickListener listener) {
        this.downloadList = downloadList;
        this.onRecorderAlbumCancelDownloadClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recorder_album_dwnload, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        FileBean fileBean = downloadList.get(position);
        String thumbPath = CacheManager.getCachePath(fileBean.path);
        File file = new File(thumbPath);
        GlideApp.with(viewHolder.itemView.getContext()).load(file).placeholder(R.mipmap.image_loading).into(((ViewHolder) viewHolder).thumbPiv);
        CarControl.CarDvThumbnailMediaInfo mediaInfo = CacheManager.getMediaInfoFromExif(thumbPath);
        ((ViewHolder) viewHolder).thumbPiv.setProgress(0);
        String size;
        String date;
        if (mediaInfo != null) {
            date = mediaInfo.file_time;
            size = DataClearManager.getFormatSize(mediaInfo.size);
        } else {
            date = "xx-xx-xx xx:xx:xx";
            size = "0M";
        }
        ((ViewHolder) viewHolder).dateTv.setText(date);
        ((ViewHolder) viewHolder).progressTv.setText(viewHolder.itemView.getResources().getString(R.string.recorder_album_download_progress) + " 0%/" + size);
        ((ViewHolder) viewHolder).cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRecorderAlbumCancelDownloadClickListener != null)
                    onRecorderAlbumCancelDownloadClickListener.downloadCancel(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return downloadList == null ? 0 : downloadList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.thumb_piv)
        public ProgressImageView thumbPiv;
        @BindView(R2.id.tv_date)
        TextView dateTv;
        @BindView(R2.id.tv_progress)
        public TextView progressTv;
        @BindView(R2.id.tv_cancel)
        TextView cancelTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
